package edu.hawaii.duedates.wicket;

import org.apache.wicket.behavior.HeaderContributor;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;



/**
 * This is the Alert page.
 * 
 * @author robin
 *
 */
public class AlertPage extends BasePage {
  
  /** Support serialization. */
  private static final long serialVersionUID = 1L;
  
  /** Boolean holder for email option. */
  boolean emailCheck = false;
  
  boolean uhmCheck = false;
  boolean hslCheck = false;
  
  /** String holder for user's email. */
  String userEmail = "";
  
  /** String holder for error label. */
  String errorLabel = "";
  

  
  /**
   * Constructor for the Alert Page.
   */
  public AlertPage() {
    
    add(HeaderContributor.forCss(edu.hawaii.duedates.wicket.AlertPage.class, 
        "style.css", "screen"));
    
    final DueDatesSession session = (DueDatesSession)getSession();
    userEmail = session.getDueDates().getUser().getEmail();

    Form form = new Form("alertPage");
    double intervalInput = 1;
    form.add(new Label("errorWebLabel", session.getErrorMessage()));
    form.add(new TextField("userInterval", new PropertyModel(session, "intervalInput")));
    form.add(new Label("emailLabel", userEmail));
    form.add(new CheckBox("emailCheckBox", new PropertyModel(this, "emailCheck")));
    
    
    form.add(new CheckBox("uhmCheckBox", new PropertyModel(this, "uhmCheck")));
    form.add(new CheckBox("hslCheckBox", new PropertyModel(this, "hslCheck")));
    
    setWakeUpInterval(intervalInput);
    
    form.add(new Button("runButton") {
      /** Support serialization */
      private static final long serialVersionUID = 1L;
      
      @Override
      public void onSubmit() {
        
        if (emailCheck) {
          try {
            checkLibrary();
            getDueDates().retrieveInfo();
            System.out.println("Email: " + emailCheck);
            System.out.println("UHM: " + uhmCheck);
            System.out.println("HSL: " + hslCheck);
            getDueDates().wakeUp(session.getInterval(), emailCheck);

          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }
        else {
          try {
            checkLibrary();
            getDueDates().retrieveInfo();
            System.out.println("Email: " + emailCheck);
            System.out.println("UHM: " + uhmCheck);
            System.out.println("HSL: " + hslCheck);
            getDueDates().wakeUp(session.getInterval(), false);
          }
          catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    });
    form.add(new Button("cancelButton") {
      
      /** Support serialization */
      private static final long serialVersionUID = 1L;

      public void onSubmit() {
        try {
          getDueDates().setCancelWakeUp();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
    add(form);
  }
  
  /**
   * This will check if the library checks are true or false.
   * 
   */
  public void checkLibrary() {
    
    if (uhmCheck) {
      getDueDates().setLibraryFilterOutHsl();
    }
    
    else if (hslCheck) {
      getDueDates().setLibraryFilterOutUhm();
    }
    
    else if (uhmCheck && hslCheck) {
      getDueDates().setLibraryFilterNone();
    }
  }
}
